<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: edit.phtml 4700 2010-08-16 16:14:34Z jam-gate.vn $
 * @since		2.0.0
 */
?>
<?php 
$this->headTitle($this->translator('service_edit_page_title'));

$this->headScript()
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/tomato/util.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.slideviewer/jquery.slideviewer.1.2.js');
?>
<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.core.css" rel="stylesheet">				
<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.resizable.css" rel="stylesheet">		
<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.dialog.css" rel="stylesheet">
<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.theme.css" rel="stylesheet">

<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.core.js"></script>		
<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.draggable.js"></script>		
<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.resizable.js"></script>
<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.dialog.js"></script>

<style>
	#list_product_dialog{
		display:none;
	}
	#table_list_product{
		width: 680px;
	}
	#table_list_product th{
	    background: none repeat scroll 0 0 #FF8C00;
	    color: black;  
	    padding-left:2px;  	
	}
	#table_list_product td{
		border : 1px solid #FF8C00;
		padding-top:3px;
		padding-bottom:3px;
		padding-left:2px;
	}
	#list_product_dialog{
		color:black;
	}
	#list_product_dialog table{
		width:100%;
	}
	#list_product_dialog table th{
		border : 1px solid #FF8C00;
	    background: none repeat scroll 0 0 #FF8C00;
	    color: black;  			
	}
	#list_product_dialog td{
		border : 1px solid #FF8C00;
		padding-top:3px;
		padding-bottom:3px;
	}
</style>

		
<div class="grid_12 t_a_ui_breadcrumb">
	<div class="grid_11 alpha">
		<span><a href="<?php echo $this->url(array(), 'service_service_list'); ?>"><?php echo $this->translator('service_list_breadcrumb'); ?></a></span>
		<span><?php echo $this->translator('service_edit_breadcrumb'); ?></span>
	</div>	
	<div class="grid_1 omega"><?php echo $this->languageSwitcher('flag', str_repeat('&nbsp;', 2)); ?></div>
</div>

<div class="grid_12"><?php echo $this->flashMessenger(); ?></div>

<form method="post" action="<?php echo $this->url(array(), 'service_service_edit'); ?>" id="editServiceForm">
<div class="grid_12 t_a_bottom"><?php echo $this->translator('service_edit_guide'); ?></div>

<div class="grid_12 t_a_bottom">
		<div class="grid_9 alpha">
			<div class="grid_4 alpha">
				<label><?php echo $this->translator('service_service_id'); ?>:</label>
				<input size="8" readonly="readonly" type="text" id="service_id" name="service_id" value="<?php echo $this->service->id;?>" class="lock"/>
				<input type="hidden" id="shop_id" name="shop_id" value="<?php echo $this->service->shop_id;?>"/>
			</div>
			<div class="grid_3 omega">
				<label><?php echo $this->translator("service_service_is_card");?></label>
				<div style="margin-top:6px;"> 
					<input type="checkbox" name="is_card" id="is_card" <?php if($this->service->is_card>0) echo 'checked="checked"';?>>
				</div>			
			</div>
			<hr/>
			<div class="grid_9">
				<label><?php echo $this->translator('service_name'); ?>:</label>
				<span>					
					<img src="<?php echo $this->APP_STATIC_SERVER;?>/images/flags/vi_VN.png"/>
					<input maxlength="255" type="text" id="name_vi_VN" name="name_vi_VN" style="width: 300px" title="<?php echo $this->service->name_vi_VN;?>"/>					
				</span>
				<div style="margin-left:160px;margin-top:5px;">				
					<span>						
						<img src="<?php echo $this->APP_STATIC_SERVER;?>/images/flags/en_US.png"/>
						<input maxlength="255" type="text" id="name_en_US" name="name_en_US" style="width: 300px" title="<?php echo $this->service->name_en_US;?>" />
						<a class="btnTranslate" href="javascript:void(0)" onclick="translateFrom('name_vi_VN','name_en_US',this);" title="<?php echo $this->translator("service_service_translate_from_vi");?>">
							<img src="<?php echo $this->APP_STATIC_SERVER;?>/images/icons/translate.png">
						</a>
					</span>
				</div>				
			</div>			
			<hr/>
			<div class="grid_9">			
				<label><?php echo $this->translator("service_service_time");?></label>
				<input style="float:left;" type="text" name="time" id="time" title="<?php echo $this->service->time?>" size="7">
				<label style="float:left;width:auto;"><?php echo $this->translator("service_service_list_minute");?></label>
			</div>
			<hr/>
			<div class="grid_9">
				<label><?php echo $this->translator("service_service_price");?></label>
				<input style="float:left;" type="text" name="price" id="price" title="<?php echo $this->service->price?>" size="7" <?php if ($this->service->block==1) echo 'readonly="readonly" class="lock"';?>>
				<label style="float:left;width:auto;"><?php echo Zend_Auth::getInstance()->getIdentity()->shop->currency;?></label>			
			</div>
			<hr/>
			<div class="grid_9">
				<label><?php echo $this->translator("service_service_description_vi_VN");?></label>
				<textarea name="description_vi_VN" id="description_vi_VN"><?php echo $this->service->description_vi_VN?></textarea>
			</div>
			<hr/>
			<div class="grid_9">
				<label><?php echo $this->translator("service_service_description_en_US");?></label> 
				<textarea name="description_en_US" id="description_en_US"><?php echo $this->service->description_en_US?></textarea>
				<a class="btnTranslate" href="javascript:void(0)" onclick="translateFrom('description_vi_VN','description_en_US',this);" title="<?php echo $this->translator("service_service_translate_from_vi");?>">
					<img src="<?php echo $this->APP_STATIC_SERVER;?>/images/icons/translate.png">
				</a>								
			</div>
			<hr/>
			<div class="grid_9">
				<input type="hidden" name="list_product" id="list_product" value="<?php echo $this->service->list_product;?>">
				<table width="680">
					<tr>
						<th style="text-align:left;vertical-align:middle;"><?php echo $this->translator("service_service_product");?></th>
						<th width="30">
							<?php if ($this->service->block<1):?>
							<a class="btnAddProductToService" href="#" title="<?php echo $this->translator("service_service_btn_add_product");?>"><img src="<?php echo $this->APP_STATIC_SERVER;?>/images/icons/add_product_to_service.png"></a>
							<?php endif;?>
						</th>
					</tr>
				</table>
				
				<table id="table_list_product" class="table_list_product">
					<tr>
						<th style="text-align:left;"><?php echo $this->translator("product_product_id","product");?></th>
						<th style="text-align:left;"><?php echo $this->translator("product_product_name","product");?></th>
						<th style="text-align:left;"><?php echo $this->translator("product_product_sale_price","product");?></th>
						<th style="text-align:left;"><?php echo $this->translator("service_product_volume_one");?></th>
						<th style="text-align:left;"><?php echo $this->translator("service_product_num_use");?></th>
						<th style="text-align:left;"><?php echo $this->translator("service_service_dose_used");?></th>
						<?php if ($this->service->block<1):?>						
						<th class="colDeleteProduct"   style="text-align:left;width:70px;"><?php echo $this->translator("service_service_action");?></th>
						<th class="colAddProduct" style="display:none;text-align:left;width:70px;"><?php echo $this->translator("service_service_action");?></th>
						<?php endif;?>
					</tr>
						<?php if(count($this->service->serviceProduct)>0):?>					
							<?php foreach($this->service->serviceProduct as $item):?>
							<tr>
								<td>
									<input type="hidden" class="product_id" value="<?php echo $item->product_id;?>"/>
									<?php echo $item->product_id;?>
								</td>
								<td>								
									<?php  $me = 'product_name_'.$this->lang;
											echo $item->$me;
									?>
								</td>
								<td><?php echo $item->sale_price.' '.Zend_Auth::getInstance()->getIdentity()->shop->currency;?></td>
								<td><span class="volume_one"><?php echo $item->volume_one;?></span> <?php echo $item->product_unit_use;?></td>
								<td><input type="text" size="3" onchange="updateDose(this);" value="<?php echo floor($item->volume_one/$item->dose_used);?>" <?php if($this->service->block==1) echo 'readonly="readonly" class="lock num_use"';?> class="num_use"> <?php echo $this->translator("service_product_num_times");?></td>
								<td><input type="text" size="5" onchange="updateNumUse(this);" value="<?php echo $item->dose_used?>" <?php if($this->service->block==1) echo 'readonly="readonly" class="lock dose"';?> class="dose"> <?php echo $item->product_unit_use;?></td>
								<?php if ($this->service->block<1):?>
								<td class="colDeleteProduct" style="text-align:center;">								
									<a href="javascript:void(0);" onclick="fnRemoveProductFromService(this);">
										<img style="width:16px;" src="<?php echo $this->APP_STATIC_SERVER;?>/images/icons/remove_icon.png">
									</a>
								</td>
								<td class="colAddProduct" style="text-align:center;display:none;">
									<input type="checkbox" class="chkSelectProducts"/>
								</td>
								<?php endif;?>
							</tr>
							<?php endforeach;?>
						<?php endif;?>
						<tr class="noProductFromService" <?php if(count($this->service->serviceProduct)>0) echo 'style="display:none;"';?>>
							<td colspan="8" style="text-align:center;">
								<?php echo $this->translator("service_service_no_product_from_service");?>
							</td>
						</tr>																		
				</table>
			</div>
			<div id="list_product_dialog">
				<table id="table_list_product_dialog">
					<tr>
						<th style="text-align:left;"><?php echo $this->translator("product_product_id","product");?></th>
						<th style="text-align:left;"><?php echo $this->translator("product_product_name","product");?></th>
						<th style="text-align:left;"><?php echo $this->translator("product_product_sale_price","product");?></th>
						<th style="text-align:left;"><?php echo $this->translator("service_product_volume_one");?></th>
						<th style="text-align:left;"><?php echo $this->translator("service_product_num_use");?></th>
						<th style="text-align:left;"><?php echo $this->translator("service_service_dose_used");?></th>
						<?php if ($this->service->block<1):?>						
						<th class="colDeleteProduct"   style="display:none;text-align:left;width:70px;"><?php echo $this->translator("service_service_action");?></th>
						<th class="colAddProduct" style="text-align:left;width:70px;"><?php echo $this->translator("service_service_action");?></th>
						<?php endif;?>
					</tr>
					<?php if(count($this->products)>0):?>					
							<?php foreach($this->products as $item):?>
							<tr>
								<td>
									<input type="hidden" class="product_id" value="<?php echo $item->id;?>"/>
									<?php echo $item->id;?>
								</td>
								<td>								
									<?php  $me = 'product_name_'.$this->lang;
											echo $item->$me;
									?>
								</td>
								<td><?php echo $item->sale_price.' '.Zend_Auth::getInstance()->getIdentity()->shop->currency;?></td>
								<td><span class="volume_one"><?php echo $item->volume_one;?></span> <?php echo $item->product_unit_use;?></td>
								<td><input type="text" size="3" class="num_use" onchange="updateDose(this);" value="1"> <?php echo $this->translator("service_product_num_times");?></td>
								<td><input type="text" size="5" class="dose" onchange="updateNumUse(this);" value="<?php echo $item->volume_one;?>"> <?php echo $item->product_unit_use;?></td>
								<?php if ($this->service->block<1):?>
								<td class="colDeleteProduct" style="text-align:center;display:none;">								
									<a href="javascript:void(0);" onclick="fnRemoveProductFromService(this);">
										<img style="width:16px;" src="<?php echo $this->APP_STATIC_SERVER;?>/images/icons/remove_icon.png">
									</a>
								</td>
								<td class="colAddProduct" style="text-align:center;">
									<input type="checkbox" class="chkSelectProducts"/>
								</td>
								<?php endif;?>
							</tr>
							<?php endforeach;?>		
						<?php endif;?>										
				</table>		
			</div>
			<br/>
			<br/>
			<br/>
			<br/>
			<hr/>
			<div class="grid_9">
				<button style="float:right;margin-right:20px;" onclick="beforeSubmit();"><span><?php echo $this->translator("service_add_save_button");?></span></button>
			</div>
		</div>
		<div class="grid_3 omega">
			<?php
				echo $this->helperLoader('core')->listImage("images",$this->service->images,"service",$this->service->id); 
			?>		    			
		</div>
</form>

<script type="text/javascript">
function updateNumUse(this_){
	var volume_one = $(this_).parent("td").prev().prev().find(".volume_one").text() * 1;
	dose    = $(this_).val() * 1;
	if(isNaN(dose) || dose==0){
		dose = 1;
		$(this_).val(1);
	}				 
	var num_use       = Math.floor(volume_one/dose); 
	var dose       = volume_one/num_use;
	$(this_).parent("td").prev().find(".num_use").val(num_use);
	$(this_).val(dose);
	
}
function updateDose(this_){
	var volume_one = $(this_).parent("td").prev().find(".volume_one").text() * 1;
	var num_use    = $(this_).val() * 1;
	if(isNaN(num_use) || num_use==0){
		num_use = 1;
		$(this_).val(1);
	}				 
	var dose       = volume_one/num_use; 
	$(this_).parent("td").next().find(".dose").val(dose);
}
function beforeSubmit(){
	var arr   = [];
	var first = $("#table_list_product tr:eq(1)");
	if(first.size()>0){		
		while(first.size()>0){
			if(!first.hasClass("noProductFromService")){
				var product_id = first.find(".product_id").val();
				var dose	   = first.find(".dose").val();
				arr.push(product_id+'-'+dose);
			}
			first = first.next();
		}	
	}
	var str = implode(',',arr);
	$("#list_product").val(str);
}
function fnRemoveProductFromService(this_){
	if(confirm("<?php echo $this->translator("service_confirm_remove_product");?>")){
		var row = $(this_).parent("td").parent("tr");		
		row.find(".colDeleteProduct").hide();
		row.find(".colAddProduct").show();	
			
		row.appendTo("#table_list_product_dialog");
		
		$("#table_list_product_dialog").find(".noProductFromService").hide();
		if($("#table_list_product").find("tr").size()<=2)
			$("#table_list_product").find(".noProductFromService").show();	
	}
}
function implode( glue, pieces ) {  
    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );  
}  

$(document).ready(function() {
	$(".btnAddProductToService").click(function(){
		$("#list_product_dialog").dialog({
			title: '<?php echo $this->translator("dialog_add_product_to_service");?>',
			dialogClass: 't_a_ui_dialog',
			position: 'center',
			modal: true,
			closeOnEscape: false,
			resizable: true,
			width: 600,
			height: 300,
			"buttons": { 
					"<?php echo $this->translator("service_btn_dialog_add_product");?>": function() {
						$("#list_product_dialog").find(".chkSelectProducts").each(function(){
							var checked = $(this).attr("checked");
							if(checked){								
								var row = $(this).parent("td").parent("tr");
								row.find(".colDeleteProduct").show();
								row.find(".colAddProduct").hide();								
								
								row.appendTo("#table_list_product");
								
								$("#table_list_product").find(".noProductFromService").hide();
								if($("#table_list_product_dialog").find("tr").size()<=2)
									$("#table_list_product_dialog").find(".noProductFromService").show();									
							}	
						});	 
						$(this).dialog("close"); 
					}
			}, 					
			close: function(event, ui) {
				$(this).dialog('destroy');                       
			},
			open: function(event, ui){
				$(".ui-dialog-buttonpane").find("button").each(function(){
					$(this).css("width","80px");
					$(this).css("padding","6px");
					$(this).css("background","#D2691E");
				});	
			}
		});	
		return false;		
	});	
	$('#editServiceForm').validate({
		rules: {
			name_vi_VN: {
				required: true,
		  		maxlength: 255
			},
			name_en_US: {
				required: true,
		  		maxlength: 255
			},
			time: {
				required:true,
				digits: true
			},
			price: {
				required:true,
				digits: true
			},
			description_vi_VN:{
				required:true,
				maxlength: 1024
			},
			description_en_US:{
				required:true,
				maxlength: 1024
			}
	    },
	    messages: {
	    	service_name_vi_VN: {
	        	required: "<?php echo $this->translator('service_name_vi_VN_required'); ?>",
				maxlength: "<?php echo $this->translator('service_name_vi_VN_maxlength'); ?>"
			},
	    	service_name_en_US: {
	        	required:  "<?php echo $this->translator('service_name_en_US_required'); ?>",
				maxlength: "<?php echo $this->translator('service_name_en_US_maxlength'); ?>"
			},
			description_vi_VN:{
				required: "<?php echo $this->translator('service_description_vi_VN_required'); ?>",
				maxlength: "<?php echo $this->translator('service_description_vi_VN_maxlength'); ?>"
			},
			description_en_US:{
				required: "<?php echo $this->translator('service_description_en_US_required'); ?>",
				maxlength: "<?php echo $this->translator('service_description_en_US_maxlength'); ?>"
			},
			time:{
				required:"<?php echo $this->translator('service_time_required'); ?>",
				digits:  "<?php echo $this->translator('service_time_is_digits'); ?>"
			},
			price:{
				required:"<?php echo $this->translator('service_price_required'); ?>",
				digits:  "<?php echo $this->translator('service_price_is_digits'); ?>"
			}
		}
	});
});
</script>